import os
from apscheduler.schedulers.blocking import BlockingScheduler
import logging
import time
import random


"""
亚马逊排行榜分析模型
每日运行一次，数据库空值不会报错，程序可以继续运行
"""


rd_time = random.randint(3, 5)

def timing():
    logging.info('爬虫程序启动开始运行')
    # tops类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 tops_blouses_button.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 tops_bodysuits.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 tops_polos.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 tops_tanks_camis.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 tops_tshirts.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 tops_tunics.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 tops_vests.py")
    time.sleep(rd_time)
    # active类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 active_hoodies.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 active_shirt_tees.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 active_shorts.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 active_sweatshirts.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 active_top_bottom.py")
    time.sleep(rd_time)
    # coats类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 coats_faux_fur.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 coats_jackets.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 coats_performance.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 coats_vests.py")
    time.sleep(rd_time)
    # dress类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 dress_casual.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 dress_club_ningtout.py")
    time.sleep(rd_time)
    # fashion类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 fashion_hoodies.py")
    time.sleep(rd_time)
    # lingerie类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 lingerie_bottoms.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 lingerie_nightgowns.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 lingerie_sets.py")
    time.sleep(rd_time)

    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 lingerie_tops.py")
    time.sleep(rd_time)
    # short类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 shorts_casual.py")
    time.sleep(rd_time)
    # sweaters类
    os.chdir("/home/ningzi/running_services/process_data/data_analysis_amazon/process_datas/")
    os.system("python3 sweaters_cardigans.py")
    time.sleep(rd_time)


if __name__ == '__main__':
    scheduler = BlockingScheduler()
    print('等待启动')
    #定时cron模式启动
    scheduler.add_job(timing, 'cron', hour=2, minute=0, second=0)
    try:
        scheduler.start()
        print('启动正常')
    except (KeyboardInterrupt, SystemExit):
        print('启动异常')

    logging.basicConfig(
        level=logging.INFO,
        filename="resources/log.txt"
    )
